package com.yumeng.example.main.controller;

import com.yumeng.common.api.ApiResult;
import com.yumeng.common.api.PageData;
import com.yumeng.common.exception.BusinessException;
import com.yumeng.common.exception.DemoException;
import com.yumeng.common.exception.YumengException;
import com.yumeng.common.test.TestModel;
import com.yumeng.common.test.bean.TestItem;
import com.yumeng.common.test.bean.TestItemResultVo;
import com.yumeng.common.utils.JacksonUtils;
import com.yumeng.example.main.dto.SimpleDto;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.ErrorResponseException;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.support.MissingServletRequestPartException;

import java.util.List;

/**
 * @author wxd
 * @date 2024/5/28 上午10:58
 */
@Slf4j
@RestController
//@ApiSupport(order = 1)
@Tag(name = "简单Controller", description = "简单接口集合")
@RequestMapping("/example/simple")
public class ExampleSimpleController {

    /**
     * 执行一些事情
     *
     * @return
     */
    //@ApiOperationSupport(order = 1)
    @Operation(summary = "执行一些事情", description = "执行一些事情的描述")
    @Parameter(name = "name", description = "测试姓名", required = true)
    @GetMapping("/doSomething.do")
    public String doSomething(String name, HttpServletRequest request){
        TestModel.doTest(null);
        request.setAttribute("TestController_doSomething", name);
        return "doSomething success " + name;
    }

    /**
     * 执行一些事情（json）
     *
     * @return
     */
    //@HttpBodyDecrypt
    //@ApiOperationSupport(order = 2)
    @Operation(summary = "执行一些事情（json）")
    @PostMapping("/doSomethingJson.do")
    public SimpleDto doSomethingJson(@RequestBody @Validated SimpleDto dto, HttpServletRequest request){
        return dto;
    }

    //@ApiOperationSupport(order = 3)
    @Operation(summary = "测试日志")
    @GetMapping("/testLog.do")
    public String testLog(String name) {

        log.trace("名称:{}-log1 trace", log.getName());
        log.debug("名称:{}-log1 debug", log.getName());
        log.info("名称:{}-log1 info", log.getName());
        log.warn("名称:{}-log1 warn", log.getName());
        log.error("名称:{}-log1 error", log.getName());

        return "testLog success";
    }

    /**
     * 测试全局返回
     *
     * @return
     */
    //@ApiOperationSupport(order = 4)
    @Operation(summary = "测试全局返回")
    @Parameter(name = "type", description = "返回类型 1-9", required = true)
    @GetMapping("/testResponseBodyAdvice.do")
    public Object testResponseBodyAdvice(Integer type){
        type = type != null ? type : 1;
        if (type == 1){
            return true;
        }else if (type == 2){
            return "String success";
        }else if (type == 3){
            return JacksonUtils.toJsonString(new TestItem("王二", "玩家", 1));
        }else if (type == 4){
            return new ResponseEntity<>("ResponseEntity success", HttpStatus.OK);
        }else if (type == 5){
            return ApiResult.successData("ApiResult success");
        }else if (type == 6){
            return List.of("item1", "item2", "item3");
        }else if (type == 7){
            return new TestItemResultVo("张三", "工程师");
        }else if (type == 8){
            return new TestItem("李四", "产品经理", 1);
        }else if (type == 9){
            return new PageData<>(33, List.of("pageItem1", "pageItem2", "pageItem3", "pageItem4"), 1, 20);
        }else {
            throw new BusinessException("返回类型异常");
        }
    }

    //@ApiOperationSupport(order = 5)
    @Operation(summary = "测试全局异常处理")
    @Parameter(name = "type", description = "返回类型 1-11", required = true)
    @GetMapping("/testGlobalExceptionHandler.do")
    public void testGlobalExceptionHandler(Integer type) throws Exception {
        type = type != null ? type : 1;
        if (type == 1){
            throw new Exception("测试 Exception Handler");
        }else if (type == 2){
            throw new RuntimeException("测试 RuntimeException Handler");
        }else if (type == 3){
            throw new BusinessException("测试 BusinessException Handler");
        }else if (type == 4){
            throw new ErrorResponseException(HttpStatus.MULTI_STATUS);
        }else if (type == 5){
            throw new HttpMediaTypeNotSupportedException("测试 HttpMediaTypeNotSupportedException Handler");
        }else if (type == 6){
            throw new HttpMediaTypeNotAcceptableException("测试 HttpMediaTypeNotAcceptableException Handler");
        }else if (type == 7){
            throw new ServletRequestBindingException("测试 ServletRequestBindingException Handler");
        }else if (type == 8){
            throw new HttpRequestMethodNotSupportedException("测试 HttpRequestMethodNotSupportedException Handler");
        }else if (type == 9){
            throw new MissingServletRequestPartException("测试 MissingServletRequestPartException Handler");
        }else if (type == 10){
            throw new HttpMessageNotWritableException("测试 HttpMessageNotWritableException Handler");
        }else if (type == 11){
            throw new DemoException("测试 DemoException Handler");
        }else {
            throw new YumengException("ExceptionHandler类型异常");
        }
    }



}
